using System;

using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace ElfBinary
{
	[Serializable]
	public class ElfBinaryFile
	{

		public ElfBinaryFile()
		{
		}

		//	public ElfHeader32 Header32 {get;set;}
		public ElfHeader64 Header64 { get; set;}

		//	public List<SectionHeader32> SectionHeaders32 { get; set; }

		public List<SectionHeader64> SectionHeaders64 { get; set; }

		public string FileName {get;set;}

		/// <summary>
		/// Corresponds to the e_ident field, specifies the file type.
		/// </summary>
		/// <param name="fileName">File name.</param>
	
	
		public ElfBinaryFile (string fileName)
		{
			FileName = fileName;
		
		
		}

		[NonSerialized]
		private System.IO.FileStream fs;

		public void Load()
		{
			fs = new System.IO.FileStream (FileName, System.IO.FileMode.Open);

			ElfIdentification Identity;

			Identity = new ElfIdentification ();

			Identity.Read (fs);

//			if (Identity.FileClass == ElfClass.Elf32Bit) {
//				Header32 = new ElfHeader32 ();
//				Header32.Identity = Identity;
//				Header32.Read (fs);
//
//				SectionHeaders32 = new List<SectionHeader32> ();
//
//
//				long lastpos = fs.Position;
//				fs.Seek ((long)Header32.SectionHeaderFileOffset, System.IO.SeekOrigin.Begin);
//
//				for (int x = 0; x < Header32.SectionHeaderEntryCount; x++) {
//				
//					SectionHeader32 header = new SectionHeader32 ();
//					header.Read (fs);
//					this.SectionHeaders32.Add (header);
//				}
//
//				SectionHeaders32.
////				Where (o => o.SectionType == 
////					ElfSectionType.StringTable).
//				ToList ().ForEach (o => {
//
//					o.LoadSectionBytes(fs);
//				});
//
//
//			
//			} else {

				Header64 = new ElfHeader64 ();
				Header64.Identity = Identity;
				Header64.Read (fs);
				
				SectionHeaders64 = new List<SectionHeader64> ();

				fs.Seek ((long)Header64.SectionHeaderFileOffset, System.IO.SeekOrigin.Begin);


				for (int x = 0; x < Header64.SectionHeaderEntryCount; x++) {

					SectionHeader64 header = new SectionHeader64 (this);

					header.Read (fs);

					this.SectionHeaders64.Add (header);

				}

				SectionHeaders64.
//				Where (o => o.SectionType == ElfSectionType.StringTable).
				ToList ().ForEach (o => {

					o.LoadSectionBytes(fs);
				});

			
			//}


			fs.Close ();
		}
	}
}

